<?php
/**
 * 腾讯云点播操作
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/2/24
 * Time: 11:37
 */

namespace App\Http\Logics\MiniProgram;

use Vod\VodApi;
use App\Http\Logics\BaseLogic;
class DianBoLogic extends BaseLogic
{
    public static function applyUpload()
    {
        $data = [
            'Action' => 'ApplyUpload',
            'videoType' => 'mp4',
            'coverType' => 'jpg',
            'Region' => config('app.tencent_cos.region'),
            'Timestamp' => time(),
            'Nonce' => rand(1,10000),
            'SecretId' => 'AKIDvkT2ResaddffU7Jr0zmOp6CC2JZl6958'
        ];
        $host = 'vod.api.qcloud.com';
        $signature = self::createSignature($host, $data);
        $data['Signature'] = $signature;
        $url = 'https://vod.api.qcloud.com/v2/index.php?'.http_build_query($data);
        dd(doGet($url));
    }

    public static function createSignature($host, $data) {
        ksort($data);
        $secretKey = '3vrt1HPLCwp0406Meoo1c7nqxmS6Zs17';
        $srcStr = 'GET'.$host.'/v2/index.php?'.http_build_query($data);
        errorLog($srcStr);
        $signature = base64_encode(hash_hmac('sha1', $srcStr, $secretKey, true));
        errorLog($signature);
        return $signature;
    }

    public static function uploadVideo($data)
    {
        // 初始化设置
        VodApi::initConf('AKIDvkT2ResaddffU7Jr0zmOp6CC2JZl6958', '3vrt1HPLCwp0406Meoo1c7nqxmS6Zs17');

        $result = VodApi::upload(
            array (
                'videoPath' => storage_path().'/app/'.$data['video_file']
            )
        );
        $data_two = [
            'fileId' => $result['data']['fileId']
        ];
        self::convertVideo($data_two);
//        self::getVideoSnapshot($data_two);
        self::getVideoInfo($data_two);
        return self::response(self::SUCCESS, $result);
    }

    public static function getVideoInfo($data)
    {
        $arr = [
            'Action' => 'GetVideoInfo',
            'fileId' => $data['fileId'],
            'Region' => config('app.tencent_cos.region'),
            'Timestamp' => time(),
            'Nonce' => rand(1,10000),
            'SecretId' => 'AKIDvkT2ResaddffU7Jr0zmOp6CC2JZl6958'
        ];
        $host = 'vod.api.qcloud.com';
        $signature = self::createSignature($host, $arr);
        $arr['Signature'] = $signature;
        $url = 'https://vod.api.qcloud.com/v2/index.php?'.http_build_query($arr);
        dd(json_decode(doGet($url)));
    }

    public static function getVideoSnapshot($data)
    {
        $arr = [
            'Action' => 'CreateSnapshotByTimeOffset',
            'fileId' => $data['fileId'],
            'definition' => 10,
            'timeOffset.0' => 1,
            'Region' => config('app.tencent_cos.region'),
            'Timestamp' => time(),
            'Nonce' => rand(1,10000),
            'SecretId' => 'AKIDvkT2ResaddffU7Jr0zmOp6CC2JZl6958'
        ];
        $host = 'vod.api.qcloud.com';
        $signature = self::createSignature($host, $arr);
        $arr['Signature'] = $signature;
        $url = 'https://vod.api.qcloud.com/v2/index.php?'.http_build_query($arr);
        doGet($url);
    }

    public static function convertVideo($data)
    {
        $arr = [
            'Action' => 'ConvertVodFile',
            'fileId' => $data['fileId'],
            'Region' => config('app.tencent_cos.region'),
            'Timestamp' => time(),
            'Nonce' => rand(1,10000),
            'SecretId' => 'AKIDvkT2ResaddffU7Jr0zmOp6CC2JZl6958'
        ];
        $host = 'vod.api.qcloud.com';
        $signature = self::createSignature($host, $arr);
        $arr['Signature'] = $signature;
        $url = 'https://vod.api.qcloud.com/v2/index.php?'.http_build_query($arr);
        doGet($url);
    }

    public static function callback($data)
    {

    }
}